/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.components.conveters;

import org.balisunrise.application.Application;
import org.balisunrise.common.PropertiesReader;
import org.balisunrise.common.Converter;



/**
 *
 * @author Glauco
 */
public class BooleanConverter implements Converter<Boolean,String>{

    private static final String BOTH, TRUE, FALSE;

    static {
        PropertiesReader pr = Application.get().getPropertiesReader();
        BOTH = pr.readProperty(PropertiesReader.messageBOTH);
        TRUE = pr.readProperty(PropertiesReader.messageTRUE);
        FALSE = pr.readProperty(PropertiesReader.messageFALSE);
    }

    @Override
    public String convertForward(Boolean value) {

        if (value == null)
            return BOTH;

        if (value)
            return TRUE;

        return FALSE;
    }

    @Override
    public Boolean convertReverse(String value) {

        if (value == null)
            return null;

        if (value.equals(TRUE))
            return true;

        if (value.equals(FALSE))
            return false;

        return null;
    }

}
